﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using BUS;
using DAL;
namespace QuanLyNhaSach
{
    public partial class Form_QLNhaXuatBan : DevComponents.DotNetBar.Office2007Form
    {
        Form_Them_CapNhat_NXB frm;
        NhaXuatBan_BUS nxb_bus;
        NhaXuatBan_DTO nxb_dto;

        public Form_QLNhaXuatBan()
        {
            InitializeComponent();            
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            frm = new Form_Them_CapNhat_NXB();
            frm.KhiThemMoi += frm_KhiThemMoi;
            frm.Action = "Insert";            
            frm.ShowDialog();
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            frm = new Form_Them_CapNhat_NXB();
            frm.KhiCapNhat += frm_KhiCapNhat;
            frm.Action = "Update";
            frm.ID = dtg_QLNXB.Rows[dong].Cells["Str_MaNXB"].Value.ToString();
            frm.ShowDialog();
        }

        private void Form_QLNhaXuatBan_Load(object sender, EventArgs e)
        {
            dtg_QLNXB.AutoGenerateColumns = false;
            LoadDanhSachNhaXuatBan();
        }
        void LoadDanhSachNhaXuatBan()
        {
            nxb_bus = new NhaXuatBan_BUS();
            dtg_QLNXB.DataSource = nxb_bus.LayDanhSachNhaXuatBan();
        }
        void frm_KhiThemMoi()
        {
            LoadDanhSachNhaXuatBan();
        }
        void frm_KhiCapNhat()
        {
            LoadDanhSachNhaXuatBan();
        }
        int dong;
        private void dtg_QLTacGia_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            dong = e.RowIndex;
        }
        List<string> MaNXB;
        
        private void btnXoa_Click(object sender, EventArgs e)
        {
            MaNXB = new List<string>();
            for (int i = 0; i < dtg_QLNXB.Rows.Count; i++)
            {
                if(dtg_QLNXB.Rows[i].Cells["CHECKBOX"].Value != null)
                    MaNXB.Add(dtg_QLNXB.Rows[i].Cells["Str_MaNXB"].Value.ToString());
            }
            if (MaNXB.Count == 0)
            {
                MessageBoxEx.Show("Bạn chưa chọn nhà xuất bản để xóa.", "Thông báo");
                return;
            }

            if (MessageBoxEx.Show("Bạn có chắc chắn xóa dòng này không?", "Xóa nhà xuất bản", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                for (int i = 0; i < MaNXB.Count; i++)
                {
                    nxb_dto = new NhaXuatBan_DTO();
                    nxb_bus = new NhaXuatBan_BUS();
                    nxb_dto.Str_MaNXB = MaNXB[i].ToString();
                    nxb_bus.XoaNXB(nxb_dto);
                }
                LoadDanhSachNhaXuatBan();
                MaNXB.Clear();
            }
            else
            {
                for (int i = 0; i < dtg_QLNXB.Rows.Count; i++)
                {
                    dtg_QLNXB.Rows[i].Cells["CHECKBOX"].Value = false;
                }
            }
        }

        private void dtg_QLTacGia_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            btnCapNhat_Click(sender, e); 
        }

        private void dtg_QLNXB_Click(object sender, EventArgs e)
        {
            if (dtg_QLNXB.Rows[dong].Cells["CHECKBOX"].Value == null)
                dtg_QLNXB.Rows[dong].Cells["CHECKBOX"].Value = true;
            else
                dtg_QLNXB.Rows[dong].Cells["CHECKBOX"].Value = null;
        }
    }
}
